/*******************************************************************************
 * Copyright (c) 2010, 2030 www.itlaobing.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package cn.itlaobing;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;





public class Demo1 {


	public static void main2(String[] args) throws IOException {
		OkHttpClient client = new OkHttpClient();

		String url = "https://www.baidu.com";

		Request request = new Request.Builder().url(url)

				.build();

		Response response = client.newCall(request).execute();

		//System.out.println(response.body().string());

		//jsoup解析html，取出HTMl中所有的Img标签，拿到标签上的 src地址，然后再使用 OKHttpClient将请求发出后保存到磁盘
	}
	public static List<String> getMailsByWeb() throws IOException{
		 //1,读取源文件。

	    //URL url=new URL("http://www.cnblogs.com/Renyi-Fan/p/6896901.html");
	    URL url=new URL("http://www.nipic.com/topic/show_27136_1.html");
	    BufferedReader bufr=new BufferedReader(new InputStreamReader(url.openStream()));
	    //2,对读取的数据进行规则的匹配。从中获取符合规则的数据.
	    String mail_regex = "(i?)<img.*? src=\\\"?(.*?\\\\.(jpg|gif|bmp|bnp|png))\\\".*? />";
	    
	    List<String> list=new ArrayList<String>();
	    Pattern p = Pattern.compile(mail_regex);
	    String line = null;
	    while((line=bufr.readLine())!=null){
	        Matcher m = p.matcher(line);
	        while(m.find()){
	        //    3,将符合规则的数据存储到集合中。
	            list.add(m.group());
	        }
	    	//list.add(line);
	    }
	    return list;   
	}
	public static void writer(List<String> list) throws IOException {
		File f=new File("D:/1.txt");
		FileWriter fw=new FileWriter(f);
		BufferedWriter bw=new BufferedWriter(fw);
		 for(String mail:list){
		        //System.out.println(mail);
		    	bw.write(mail);
		    }
		    bw.close();
		    fw.close();
	}
	public static void main(String[] args) throws IOException {
		/*File f=new File("D:/1.txt");
		FileWriter fw=new FileWriter(f);
		BufferedWriter bw=new BufferedWriter(fw);*/
		

	    List<String> list=getMailsByWeb();
	    writer(list);

	}

}
